--
module("UI.Windows.AddBankCardWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local GameObject = UnityEngine.GameObject
local json = require("cjson")
AddBankCardWin = UIWindow:new()

local atools = require("StringTool").StringTool

AddBankCardWin.name = "AddBankCardWin"


function AddBankCardWin:Init()
    local t = self.Container.transform
    -----验证码
    ---
    self.close_btn = UIUtility.GetChildTransform("close_btn", t, true)
    -----验证码
    self:BindEvents()
    --获取银行列表
    self.allBankList = {}
    self.xzyh_grid = UIUtility.GetChildTransform("xzyh_grid", t, true)
    self.xzyh_grid1 = UIUtility.FindContorl("UIGrid", "xzyh_grid1", t)
    self.xzyh_grid_item = UIUtility.GetChildTransform("xzyh_grid_item", self.xzyh_grid.transform, true)
    self.xzyh_btn_lable = UIUtility.FindContorl("UILabel", "xzyh_btn", t)
    require("NetWork.NetHttp").SendGetRequest("/byr/payment/constant", function(ok, data)
        if ok then
            print("获取银行列表" .. stringify(data))
            self.allBankList = data.data.bank
            for i = 1, #self.allBankList do
                local item = GameObject.Instantiate(self.xzyh_grid_item)
                UIUtility.FindContorl("UIToggle", "xzyh_lable1", item.transform).onChange:Clear()
                UIUtility.FindContorl("UIToggle", "xzyh_lable1", item.transform).onChange:Add(UIHelper
                    .CreateEventDelegate(function()
                        if UIUtility.FindContorl("UIToggle", "xzyh_lable1", item.transform).value then
                            --self.xzyh_grid.parent.gameObject:SetActive(false)
                            self.xzyh_btn_lable.text = self.allBankList[i].name .. " >"
                        end
                    end, item.gameObject))

                UIHelper.BindUIEvnet("Click", function()
                    self.xzyh_grid.parent.gameObject:SetActive(false)
                end, UIUtility.GetChildTransform("xzyh_lable1", item.transform, true).gameObject)
                item.transform.parent = self.xzyh_grid1.transform
                item.transform.localScale = Vector3.one
                item.transform.localPosition = Vector3.zero
                item.transform.localEulerAngles = Vector3.zero
                item.gameObject:SetActive(true)
                UIUtility.FindContorl("UILabel", "xzyh_lable1", item.transform).text = self.allBankList[i].name
                UIUtility.FindContorl("UILabel", "xzyh_lable2", item.transform).text = self.allBankList[i].name
                if i == 1 then
                    UIUtility.FindContorl("UIToggle", "xzyh_lable1", item.transform).value = true
                end
            end
            self.xzyh_grid1.repositionNow = true
            self.xzyh_grid1:Reposition()
        else
            print(stringify(data))
        end
    end)



    --选择银行点击
    self.xzyh_btn = UIUtility.GetChildTransform("xzyh_btn", t, true)
    UIHelper.BindUIEvnet("Click", function()
        print("选择银行")
        if self.xzyh_grid.parent.gameObject.activeSelf then
            self.xzyh_grid.parent.gameObject:SetActive(false)
        else
            self.xzyh_grid.parent.gameObject:SetActive(true)
        end
    end, self.xzyh_btn.gameObject)

    self.queren_btn = UIUtility.GetChildTransform("queren_btn", t, true)
    self.bank_user_name_input = UIUtility.FindContorl("UIInput", "bank_user_name_input", t)
    self.xzyh_btn_lable = UIUtility.FindContorl("UILabel", "xzyh_btn", t)
    self.bank_cardNumber_input = UIUtility.FindContorl("UIInput", "bank_cardNumber_input", t)
    self.bank_passWord_input = UIUtility.FindContorl("UIInput", "bank_passWord_input", t)

    self.isAddCard = true
    self.curBankId = 0
    self.Data = {}


    UIHelper.BindUIEvnet("Click", function()
        local saveBankJson
        -- saveBankJson = string.format('{"withdrawId":%s,"withdrawMethod": 1,"accountName":"%s","bankName": "%s","cardNumber": "%s","secondPassword": "%s"}',
        --self.bank_user_name_input.value, self.xzyh_btn_lable.text, self.bank_cardNumber_input.value, self.bank_passWord_input.value)
        if self.isAddCard then
            saveBankJson = string.format(
                '{"withdrawMethod": 1,"accountName":"%s","bankName": "%s","cardNumber": "%s","secondPassword": "%s"}',
                self.bank_user_name_input.value, atools:Split(self.xzyh_btn_lable.text, " >")[1],
                self.bank_cardNumber_input.value,
                self.bank_passWord_input.value)
        else
            saveBankJson = string.format(
                '{"withdrawId":%s,"withdrawMethod": 1,"accountName":"%s","bankName": "%s","cardNumber": "%s","secondPassword": "%s"}',
                self.curBankId, self.bank_user_name_input.value, atools:Split(self.xzyh_btn_lable.text, " >")[1],
                self.bank_cardNumber_input.value, self.bank_passWord_input.value)
        end
        require("NetWork.NetHttp").SendPostRequest_token("/byr/payment/withdraw/saveOrUpdate", function(ok, data)
            if ok then
                print("添加银行卡" .. stringify(data))
                UIWinMgr:OpenTextNotice("银行卡设置成功")

                local NewGameLoobyWin = require("UI.Windows.NewGameLoobyWin").NewGameLoobyWin
                NewGameLoobyWin.user_data_bankinfo = self.Data
                NewGameLoobyWin.secondPassword = self.bank_passWord_input.value
                self:Close(1)
            else
                print(stringify(data))
                UIWinMgr:OpenTextNotice(data.msg)
            end
        end, saveBankJson)
    end, self.queren_btn.gameObject)
end

function AddBankCardWin:OnShow()
    --获取已存储的信息
    require("NetWork.NetHttp").SendGetRequest("/byr/payment/withdraw/info", function(ok, data)
        if ok then
            print("获取用户提现信息" .. stringify(data))
            for i = 1, #data.data do
                if i==1  then
                    self.isAddCard = false
                    self.curBankId = data.data[i].withdrawId
                    self.Data = data.data[i]
                    self.bank_user_name_input.value = self.Data.accountName
                    self.xzyh_btn_lable.text = self.Data.bankName .. " >"
                    self.bank_cardNumber_input.value = self.Data.cardNumber

                    break
                end
            end
        else
            print(stringify(data))
        end
    end)
end

function AddBankCardWin:OnClose()

end

function AddBankCardWin:BindEvents()
    UIHelper.BindUIEvnet("Click", function()
        self:Close(1)
    end, self.close_btn.gameObject)
end
